package com.freemark;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.SimpleHash;
import freemarker.template.Template;




/**
 * @author qintao
 *
 */
public class FreeMarkerTool {
	
	//private final static String COMMON_PACKAGE_PATH ="com.";
	
	
	private String packageName;
	private String pojo;
	
	
	
	public String getPackageName() {
		return packageName;
	}


	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}


	public String getPojo() {
		return pojo;
	}


	public void setPojo(String pojo) {
		this.pojo = pojo;
	}

	
	public FreeMarkerTool() {
		super();
	}

	public FreeMarkerTool(String packageName, String pojo) {
		super();
		this.packageName = packageName;
		this.pojo = pojo;
	}


	private static Configuration initFreeTamplateConfiguration(String url) throws IOException{
		
		Configuration cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File(FreeMarkerTool.class.getResource("/"+url).getFile()));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		return cfg;
	}
	
	public void generateJavaCode(String javaUrl,Configuration cfg,SimpleHash root) {
		try{
			String path = "";
			String className ="";
			if(javaUrl.equals("action.tfl")){
				className =this.pojo+ "Action";
				path ="action";
			}
			else if(javaUrl.equals("biz.tfl")){
				className=this.pojo+ "Biz";
				path="biz/imp";
			}
			else if(javaUrl.equals("ibiz.tfl")){
				className="I"+this.pojo+ "Biz";
				path="biz";
			}
			else if(javaUrl.equals("dao.tfl")){
				className=this.pojo+ "Dao";
				path="dao/imp";
			}
			else if(javaUrl.equals("idao.tfl")){
				className="I"+this.pojo+ "Dao";
				path="dao";
			}
			root.put("package", this.packageName+"."+path.replace("/", "."));
			
			Template template = cfg.getTemplate(javaUrl);
	
			File fileExists = new File(Thread
					.currentThread().getContextClassLoader().getResource("").getFile()+"../../../src/com/kingtake/dzgl/"+path);
			if(!fileExists.exists())
				fileExists.mkdirs();
			File file = new File(Thread
					.currentThread().getContextClassLoader().getResource("").getFile()+"../../../src/com/kingtake/dzgl/"+path+"/"+className+".java");
			
			BufferedWriter writer =null;
			writer = new BufferedWriter(new FileWriter(file));
			template.process(root, writer);
			writer.flush();
			writer.close();
			
			System.out.println("generateJavaCode: "+className+".java  successfully!");
		}catch(Exception e){
			System.out.println("generateJavaCode not  successfully!");
		}
	}

	
	public void generateSpringOrStrutsCode(String springUrl,Configuration cfg,SimpleHash root,String codeDestFileUrl){
		try {
			Template template = cfg.getTemplate(springUrl);
			root.put("package", this.packageName);
			String tempFilePath = "c:/templateFile.txt";
			File tempFile = new File(tempFilePath);
			BufferedWriter writer =null;
			writer = new BufferedWriter(new FileWriter(tempFile));
			template.process(root, writer);
			writer.flush();
			writer.close();
			
			String tempFileString = FileUtils.readFileToString(tempFile);	
			
			File codeDestFile = new File(codeDestFileUrl);
			String codeDestFileString = FileUtils.readFileToString(codeDestFile);	
			codeDestFileString = codeDestFileString.replaceAll(tempFileString, "");
			codeDestFileString = codeDestFileString.replaceAll("<!--FillMe-->", tempFileString+"\n\r<!--FillMe-->");
			FileUtils.writeStringToFile(codeDestFile, codeDestFileString);
			FileUtils.deleteQuietly(tempFile);

			
			System.out.println("generateSpringOrStrutsCode   successfully!");
		} catch (Exception e) {
			System.out.println("generateSpringOrStrutsCode not  successfully!");
		}
		
		
	}

	private SimpleHash initSimpleHash() {
		SimpleHash root = new SimpleHash();
		String pojoDao = this.pojo.substring(0,1).toLowerCase()+pojo.substring(1,pojo.length());	
		//root.put("package", this.packageName);
		root.put("pojo", this.pojo);
		root.put("pojoForBiz", pojoDao);
		return root;
	}
	
	public static void main(String[] args) {
	
		try {
			String testPackage = "com.kingtake.dzgl";
			String pojo = "AddressMp";
			FreeMarkerTool tool = new FreeMarkerTool(testPackage,pojo);
			Configuration cfg = initFreeTamplateConfiguration("template");
			tool.generateJavaCode( "action.tfl",cfg, tool.initSimpleHash());
			tool.generateJavaCode( "biz.tfl",cfg, tool.initSimpleHash());
			tool.generateJavaCode( "ibiz.tfl",cfg, tool.initSimpleHash());
			tool.generateJavaCode( "dao.tfl",cfg, tool.initSimpleHash());
			tool.generateJavaCode( "idao.tfl",cfg, tool.initSimpleHash());
			String codeDestFileSpringPath = Thread
			.currentThread().getContextClassLoader().getResource("").getFile()+"../../../WebRoot/WEB-INF/applicationContext_pro.xml";
			tool.generateSpringOrStrutsCode( "spring.tfl",cfg, tool.initSimpleHash(),codeDestFileSpringPath);
			String codeDestFileStrutsPath = Thread
			.currentThread().getContextClassLoader().getResource("").getFile()+"../../../WebRoot/WEB-INF/struts-config-dzgl.xml";
			tool.generateSpringOrStrutsCode( "struts.tfl",cfg, tool.initSimpleHash(),codeDestFileStrutsPath);
			
		
			
			
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
	}
	
	
	
}
